<template>
  <div class="mod-config">
    <basic-container>
      <el-form :inline="true" v-show="searchShow" ref="searchForm" :model="dataForm" @keyup.enter.native="searchChange">
        <el-form-item label="变更单据" prop="code">
          <el-input v-model="dataForm.chargeBillId" placeholder="变更单据" size="mini"></el-input>
        </el-form-item>
        <el-form-item label="变更日期">
          <el-row>
            <el-col :span="11">
              <el-date-picker v-model="dataForm.createTimeStart" type="datetime" value-format="yyyy-MM-dd HH:mm:ss" style="width: 100%;"></el-date-picker>
            </el-col>
            <el-col class="line" :span="2" style="text-align: center;">-</el-col>
            <el-col :span="11">
              <el-date-picker v-model="dataForm.createTimeEnd" type="datetime" style="width: 100%;" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
            </el-col>
          </el-row>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="searchChange">查询</el-button>
          <el-button type="default" icon="el-icon-delete" size="mini" @click="searchReset">清空</el-button>
        </el-form-item>
      </el-form>
      <el-row>
        <el-col :span="24">
          <el-button icon="el-icon-search" circle size="mini" @click="handleSearchShow" style="float:right;"></el-button>
          <el-button icon="el-icon-refresh" circle size="mini" @click="refreshChange" style="float:right;"></el-button>
        </el-col>
      </el-row>
      <div class="avue-crud">
        <el-table :data="dataList" border v-loading="dataListLoading" show-summary :summary-method="getSummaries">
          <el-table-column prop="goodsCode" label="编码"></el-table-column>
          <el-table-column prop="goodsBarcode" label="条形码"></el-table-column>
          <el-table-column prop="viewGoodsName" label="展示名称"></el-table-column>
          <el-table-column prop="chargeBillId" label="变更单据"></el-table-column>
          <el-table-column prop="logType" label="单据类型">
            <template slot-scope="scope">{{logTypeFormatter(scope.row.logType)}}</template>
          </el-table-column>
          <el-table-column prop="createTime" label="变更时间"></el-table-column>
          <el-table-column prop="stockName" label="仓位"></el-table-column>
          <el-table-column prop="goodsQuantity" label="数量"></el-table-column>
        </el-table>
      </div>

      <div class="avue-crud__pagination">
        <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
          :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage" background layout="total, sizes, prev, pager, next, jumper"></el-pagination>
      </div>
    </basic-container>
  </div>
</template>

<script>
  import { getStorageLog } from "@/api/eims/storage-log";
  import { mapGetters } from "vuex";
  import { filterParams } from "@/util/tools";

  export default {
    data() {
      return {
        dataForm: {
          key: ""
        },
        dataList: [],
        searchShow: true,
        pageIndex: 1,
        pageSize: 100,
        totalPage: 0,
        dataListLoading: false
      };
    },
    computed: {
      ...mapGetters(["permissions","bizData"])
    },
    methods: {
      init(info,time) {
        this.visible = true
        if (time) {
          this.$set(this.dataForm, 'createTimeStart', time.createTimeStart)
          this.$set(this.dataForm, 'createTimeEnd', time.createTimeEnd)
        } else {
          this.$set(this.dataForm, 'createTimeStart', '')
          this.$set(this.dataForm, 'createTimeEnd', '')
        }
        this.$nextTick(() => {
          // this.$refs['dataForm'].resetFields()
          if (info.ctgId == 4 || info.ctgId == 5) {
            this.dataForm.batch = info.batch;
          }
          this.dataForm.stockId = info.stockId;
          this.dataForm.goodsId = info.goodsId;
          this.getDataList()
        })
      },
      // 获取数据列表
      getDataList() {
        this.dataListLoading = true;
        let params1 = filterParams(this.dataForm);
        let params2 = {
          current: this.pageIndex,
          size: this.pageSize
        };
        getStorageLog(Object.assign(params2, params1)).then(response => {
          if(response.data.code == '0'){
            this.dataList = response.data.data.records
            this.totalPage = response.data.data.total
          }else{
            this.$message.error(response.data.msg)
          }
          this.dataListLoading = false
        });

      },
      // 每页数
      sizeChangeHandle(val) {
        this.pageSize = val;
        this.pageIndex = 1;
        this.getDataList();
      },
      // 当前页
      currentChangeHandle(val) {
        this.pageIndex = val;
        this.getDataList();
      },
      searchChange() {
        this.pageIndex = 1;
        this.getDataList();
      },
      // 搜索清空
      searchReset() {
        for (var key in this.dataForm) {
          if (key == 'stockId' || key == 'goodsId') {
            this.dataForm[key] = this.dataForm[key]
          } else {
            this.dataForm[key] = ''
          }
        }
        // this.getDataList(this.dataForm,this.page);
      },
      handleSearchShow() {
        this.searchShow = !this.searchShow;
      },
      /**
       * 刷新回调
       */
      refreshChange() {
        this.getDataList(this.page);
      },
      logTypeFormatter(value) {
        let obj = {}
        obj = this.bizData.billType.find(item => {
          return item.value == value
        })
        if(obj && obj.label){
          return obj.label
        }
      },
      //  合计
      getSummaries(param) {
        const {
          columns,
          data
        } = param;
        const sums = [];
        columns.forEach((column, index) => {
          const values = data.map(item => Number(item[column.property]));
          if (!values.every(value => isNaN(value))) {
            if (index == 7) {
              sums[index] = '合计： ' + values.reduce((prev, curr) => {
                const value = Number(curr);
                if (!isNaN(value)) {
                  return prev + curr;
                } else {
                  return prev;
                }
              }, 0)
            }
          }
        });
        return sums;
      },
    }
  };
</script>
<style lang="scss" scoped>
  /deep/ .el-table {
    display: flex;
    flex-direction: column;
  }

  /deep/ .el-table__body-wrapper {
    order: 1;
  }

  /deep/ .bgChange {
    color: red !important;
  }
</style>
